﻿using System;
using System.Collections.Generic;

namespace Framework.DataObjects
{
    public class NameValueDictionary : Dictionary<string, string>
    {
        #region C'tor
        public NameValueDictionary()
            : base(FrameworkConstant.DEFAULT_COMPARER_IGNORECASE)
        {
        }

        public NameValueDictionary(List<NameValueDo> nameValuePairs)
            : this()
        {
            if (Is.NotEmptyArray(nameValuePairs))
            {
                foreach (var pair in nameValuePairs)
                {
                    Add(pair.Name, pair.Value);
                }
            }
        }

        public NameValueDictionary(List<NameTextValueDo> nameValuePairs)
            : this()
        {
            if (Is.NotEmptyArray(nameValuePairs))
            {
                foreach (var pair in nameValuePairs)
                {
                    Add(pair.Name, pair.Value);
                }
            }
        } 
        #endregion

        #region GetValue 
        /// <summary>
        /// Lookup [String] value from dictionary using <c>key</c>, if not found then returns string.Empty.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public string GetValue(string key)
        {
            return GetValue(key, string.Empty);
        }

        /// <summary>
        /// Lookup [String] value from dictionary using <c>key</c>, if not found then returns <c>defaultValue</c>.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        public string GetValue(string key, string defaultValue)
        {
            if (ContainsKey(key))
            {
                return StrUtil.DefaultValue(this[key], defaultValue);
            }
            return defaultValue;
        }

        /// <summary>
        /// Lookup [DateTime] value from dictionary using <c>key</c>, if not found then returns DateTime.MinValue.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public DateTime GetValueAsDateTime(string key)
        {
            return GetValueAsDateTime(key, DateTime.MinValue);
        }

        /// <summary>
        /// Lookup [DateTime] value from dictionary using <c>key</c>, if not found then returns <c>defaultValue</c>.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        public DateTime GetValueAsDateTime(string key, DateTime defaultValue)
        {
            if (ContainsKey(key))
            {
                return DateUtil.ToDateTime(this[key], defaultValue);
            }
            return defaultValue;
        }

        /// <summary>
        /// Lookup [Int] value from dictionary using <c>key</c>, if not found then returns <c>-1</c>.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public int GetValueAsInt(string key)
        {
            return GetValueAsInt(key, -1);
        }

        /// <summary>
        /// Lookup [Int] value from dictionary using <c>key</c>, if not found then returns <c>defaultValue</c>.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        public int GetValueAsInt(string key, int defaultValue)
        {
            if (ContainsKey(key))
            {
                return NumUtil.ParseInt(this[key], defaultValue);
            }
            return defaultValue;
        }

        /// <summary>
        /// Lookup [Long] value from dictionary using <c>key</c>, if not found then returns <c>-1</c>.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public long GetValueAsLong(string key)
        {
            return GetValueAsLong(key, -1);
        }

        /// <summary>
        /// Lookup [Long] value from dictionary using <c>key</c>, if not found then returns <c>defaultValue</c>.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        public long GetValueAsLong(string key, long defaultValue)
        {
            if (ContainsKey(key))
            {
                return NumUtil.ParseLong(this[key], defaultValue);
            }
            return defaultValue;
        }

        /// <summary>
        /// Lookup [Decimal] value from dictionary using <c>key</c>, if not found then returns <c>-1</c>.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public decimal GetValueAsDecimal(string key)
        {
            return GetValueAsDecimal(key, -1);
        }

        /// <summary>
        /// Lookup [Decimal] value from dictionary using <c>key</c>, if not found then returns <c>defaultValue</c>.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        public decimal GetValueAsDecimal(string key, decimal defaultValue)
        {
            if (ContainsKey(key))
            {
                return NumUtil.ParseDecimal(this[key], defaultValue);
            }
            return defaultValue;
        }

        /// <summary>
        /// Lookup [Boolean] value from dictionary using <c>key</c>, if not found then returns <c>false</c>.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public bool GetValueAsBool(string key)
        {
            return GetValueAsBool(key, false);
        }

        /// <summary>
        /// Lookup [Boolean] value from dictionary using <c>key</c>, if not found then returns <c>defaultValue</c>.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="defaultValue">if set to <c>true</c> [default value].</param>
        /// <returns></returns>
        public bool GetValueAsBool(string key, bool defaultValue)
        {
            if (ContainsKey(key))
            {
                var value = this[key];

                return Is.EmptyString(value) ? defaultValue : Is.True(value);
            }
            return defaultValue;
        }
        #endregion
    }
}
